class LinkSweeper < ActionController::Caching::Sweeper

  observe Link,Product,Article,ArticleCategory,Navigation,Webconfig,ListItem,Advertisement,TuangouPro,Cycle

  def after_update(record)
    expire_fragment_cache(record)
    #expire_fragment_cache_for_show(record)
  end
  def after_create(record)
    expire_fragment_cache(record)
  end
  def after_destroy(record)
    expire_fragment_cache(record)
    #expire_fragment_cache_for_show(record)
  end


  private
  #
  def expire_fragment_cache_for_show(record)
#    case record
#    when Product
#      expire_fragment(:controller=>"products",:action=>"show")
#    when TuangouPros
#      expire_fragment(:controller=>"tuangou_pros",:action=>"show")
#    when Article
#      expire_fragment(:controller=>"articles",:action=>"show")
#    end
  end
  def expire_fragment_cache(record)
    case record
    when Link      
      expire_fragment(:controller=>"links",:action=>"list",:part=>"for_index")
      #logger.info "#{record.id} done!"
      # expire_fragment(%r{links/list.part=for_index.cache})
    when Product
      expire_fragment(:controller=>"products",:action=>"list",:part=>"for_index")
      #expire_fragment(:controller=>"products",:action=>"list",:part=>record.procate_id)
      expire_fragment(:controller=>"products",:action=>"list",:part=>"tejia")
    when Procate
      expire_fragment(:controller=>"procates",:action=>"list")
    when Cycle
      expire_fragment(:controller=>"cycles",:action=>"list")
    when Article
      expire_fragment( :controller=>"articles",:action=>"forindex",:part=>"top_3_news")
      expire_fragment( :controller=>"articles",:action=>"forindex",:part=>"top_news")
      expire_fragment( :controller=>"articles",:action=>"forindex",:part=>"cate_news")
    when ArticleCategory
      expire_fragment(:controller=>"articles",:action=>"forindex",:part=>"cate_news")
      expire_fragment(:controller=>"article_categories",:action=>"list")
    when Navigation
      expire_fragment(:controller=>"navigations",:action=>"list",:part=>"top_nav")
      expire_fragment(:controller=>"navigations",:action=>"list",:part=>"menu")
    when Webconfig
      expire_fragment(:controller=>"webconfigs",:action=>"list")
    when ListItem
      #expire_fragment( :controller=>"list_items",:action=>"list",:part=>"pinpai")
      #expire_fragment(:controller=>"list_items",:action=>"list",:part=>"pronav" )
      #expire_fragment(:controller=>"list_items",:action=>"list",:part=>/\d+/)can't use
      expire_fragment(%r{list_items/list.*})
      expire_fragment(:controller=>"attributes",:part=>"search"  )
    when Advertisement
      # expire_fragment(%r{advertisements/list\.part=index_middle.*})
      #expire_fragment(%r{advertisements/list.*})
      expire_fragment(:controller=>"advertisements",:action=>"list",:part=>record.name)
    when TuangouPro
      expire_fragment(:controller=>"tuangou_pros",:action=>"list",:part=>"top_tuangou")
    end
  end
end
